package com.pulanit.pangu.admin.gen;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.pulanit.pangu.admin.common.utils.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器支持自定义模版
 *
 * @author 天鸣
 */
public class BrowserVelocityTemplateEngine extends VelocityTemplateEngine {


    private String apiPrefix;

    private String webPrefix;

    public BrowserVelocityTemplateEngine(String apiPrefix, String webPrefix) {
        this.apiPrefix = apiPrefix;
        this.webPrefix = webPrefix;
    }

    @Override
    protected void outputCustomFile(List<CustomFile> customFiles, TableInfo tableInfo, Map<String, Object> objectMap) {

        customFiles.forEach(customFile -> {
            String key = customFile.getFileName();
            String value = customFile.getTemplatePath();
            String entityName = tableInfo.getEntityName();
            String entityNameLower = entityName.toLowerCase();
            String outputPath = getPathInfo(OutputFile.parent);
            if (GenConstant.ADD.equals(key)) {
                outputPath = webPrefix + "views/business/" + entityNameLower + "/save.vue";
            }
            if (GenConstant.INDEX.equals(key)) {
                outputPath = webPrefix + "views/business/" + entityNameLower + "/index.vue";
            }
            if (GenConstant.API.equals(key)) {
                outputPath = webPrefix + "api/model/" + entityNameLower + ".js";
            }
            if (GenConstant.PAGE.equals(key)) {
                outputPath = apiPrefix +"param/"+ entityName + "PageIn" + StringPool.DOT_JAVA;
            }
            outputFile(new File(String.valueOf(outputPath)), objectMap, value, Boolean.FALSE);
        });
    }


}